package com.chixing.controller;

import com.chixing.util.UploadImgUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Controller
@RequestMapping("test")
public class UploadFileController {

    @PostMapping("upload")
    @ResponseBody
    public String upload(@RequestParam("myimage") MultipartFile multipartFile){
        System.out.println("multipartFile:" + multipartFile);
        String imgUrl = null;
        try {
            // 返回上传文件 在七牛云服务器的URL
            imgUrl = UploadImgUtil.upload(multipartFile.getBytes(),multipartFile.getOriginalFilename());
            System.out.println("返回上传文件 在七牛云服务器的URL:" + imgUrl);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return imgUrl;
    }
}
